package com.app.utils;


import com.app.Application;
import com.app.cache.SecondLevelCache;
import com.app.constant.KeyValue;
import com.app.model.bean.User;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.Map;


/**
 *
 */

public class LoginUtil {

    private static final String LOG_TAG = LoginUtil.class.getSimpleName();


    /**
     * 检查token，如果token存在则表示已经登录，不存在则前往登录页面
     */
    public static boolean isLogin() {
        return !StringUtil.isEmpty(getToken());
    }

    // 获取token信息
    public static String getToken() {
        String token = null;
        if (SecondLevelCache.sharedInstance().get(KeyValue.TOKEN) != null) {
            token = SecondLevelCache.sharedInstance().get(KeyValue.TOKEN).toString();
        }
        return token;
    }

    // 获取登录用户名
    public static String getLoginName() {
        String loginName = null;
        if (SecondLevelCache.sharedInstance().get(KeyValue.LOGIN_NAME) != null) {
            loginName = SecondLevelCache.sharedInstance().get(KeyValue.LOGIN_NAME).toString();
        }
        return loginName;
    }

    // 退出登录，清除本地缓存的token信息和个人信息
    public static void logout() {
        SecondLevelCache.sharedInstance().clear(KeyValue.TOKEN);
        SecondLevelCache.sharedInstance().clear(KeyValue.USER);
        MobclickAgent.onProfileSignOff();
        Map<String, String> map_value = new HashMap<String, String>();
        map_value.put("账号", LoginUtil.getLoginName());
        MobclickAgent.onEventValue(Application.instance, "logOut" , map_value, 1);
    }

    // 获取token信息
    public static User getLoginUser() {
        User user = null;
        if (SecondLevelCache.sharedInstance().get(KeyValue.USER) != null) {
            user = (User)SecondLevelCache.sharedInstance().get(KeyValue.USER);
        }
        return user;
    }

}
